package com.xishu.client.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 短信客户端
 */
public class AliyunSMSClient implements IsmsClient {
    private static AliyunSMSClient instance = new AliyunSMSClient();
    private static Logger logger = LoggerFactory.getLogger(AliyunSMSClient.class);

    private AliyunSMSClient() {
    }

    public static AliyunSMSClient getInstance() {
        return instance;
    }

    /**
     * 发送短信
     *
     * @param areaCode 区号
     * @param phone    电话号码
     * @param content  内容
     */
    public void send(String areaCode, String phone, String content) throws Exception {
        //初始化acsClient，<accessKeyId>和<accessSecret>"在短信控制台查询即可
        DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "LTAI5tQNNxKE86csecB3f2v1", "ys771qzh9qkaBph9r3ueYSwYeAHFyf");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //域名，请勿修改
        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        //API版本号，请勿修改
        request.setSysVersion("2018-05-01");
        //API名称
        request.setSysAction("SendMessageToGlobe");
        //接收号码，格式为：国际码+号码，必填
        request.putQueryParameter("To", areaCode + phone);
        //发送方senderId，选填
        //request.putQueryParameter("From", "1234567890");
        //短信内容，必填
        request.putQueryParameter("Message", content);
        CommonResponse response = client.getCommonResponse(request);
        logger.info("sms result is {}", response.getData());
    }

    public static void main(String[] args) throws Exception {
        AliyunSMSClient.getInstance().send("852", "67608601", "have a test");
    }
}
